package com.service.abolishing.rabbitmq;

/**
 * @ClassName: MsgProducer
 * @Description:
 * @Author guochuang
 * @Date 2021/10/25
 * @Version 1.0
 */

import com.service.abolishing.config.RabbitConfig;
import com.service.abolishing.dto.DeviceTerminalUser;
import com.service.abolishing.entity.ApDetail;
import com.service.abolishing.entity.CloudCampusInfo;
import com.service.config.dto.MailBean;
import com.service.config.utils.JsonXMLUtils;
import com.service.config.utils.RedisUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.AmqpException;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.stereotype.Component;

import java.util.UUID;

//消息的生产者
@Component
public class MsgProducer implements RabbitTemplate.ConfirmCallback {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    //由于rabbitTemplate的scope属性设置为ConfigurableBeanFactory.SCOPE_PROTOTYPE，所以不能自动注入
    //private RabbitTemplate rabbitTemplate;
    @Autowired
    private RabbitTemplate rabbitTemplate;
    @Autowired
    RedisUtil redisUtil;
    /**
     * 构造方法注入rabbitTemplate
     */
    @Autowired
    public MsgProducer(RabbitTemplate rabbitTemplate) {
        this.rabbitTemplate = rabbitTemplate;
        rabbitTemplate.setConfirmCallback(this); //rabbitTemplate如果为单例的话，那回调就是最后设置的内容
    }

    public void sendMsg(MailBean mailBean, int type) {
        try{
            CorrelationData correlationId = new CorrelationData(UUID.randomUUID().toString());
            //把消息放入ROUTINGKEY_A对应的队列当中去，对应的是队列A
            if (type == 1) { // 周四发送项目和事务邮件
                logger.info(">>>>>>>>>>>>>>require is begin："+mailBean.getRecipient());
                rabbitTemplate.convertAndSend(RabbitConfig.EXCHANGE_A, RabbitConfig.ROUTING_KEY_A, JsonXMLUtils.obj2json(mailBean),correlationId);
            }
            logger.info("请求已入队列>>>>>>>>>>>>>");
        } catch (AmqpException e) {
            logger.error("请求发送异常："+e.getMessage());
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    /**
     * 回调
     */
    @Override
    public void confirm(CorrelationData correlationData, boolean ack, String cause) {
        logger.info(" 回调id:" + correlationData);
        if (ack) {
            logger.info("消息成功消费:"+correlationData.getId());
        } else {
            logger.info("消息消费失败:" + cause);
        }
    }

    public void getByDeviceTerminalUserList(String online, ApDetail apDetails1, HttpHeaders token, CloudCampusInfo cloudCampusInfo,String timeStamp) {
        try{
            CorrelationData correlationId = new CorrelationData(UUID.randomUUID().toString());
            //ROUTING_KEY_C，对应的是队列A
            if (apDetails1 != null) { // 周四发送项目和事务邮件
                logger.info(">>>>>>>>>>>>>>require is begin：");
                DeviceTerminalUser deviceTerminalUser = new DeviceTerminalUser();
                deviceTerminalUser.setStatus(online);
                deviceTerminalUser.setTimeStamp(timeStamp);
                deviceTerminalUser.setApDetails(apDetails1);
                deviceTerminalUser.setCloudCampusInfo(cloudCampusInfo);
                deviceTerminalUser.setToken(token);
                rabbitTemplate.convertAndSend(RabbitConfig.EXCHANGE_A, RabbitConfig.ROUTING_KEY_C, JsonXMLUtils.obj2json(deviceTerminalUser),correlationId);
            }
            logger.info("请求已入队列>>>>>>>>>>>>>");
        } catch (AmqpException e) {
            logger.error("请求发送异常："+e.getMessage());
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
